package com.metis.document.parse.dialog.utils.ikseg;

import com.metis.document.parse.dialog.utils.ikseg.cfg.IkConfiguration;
import com.metis.document.parse.dialog.utils.ikseg.core.IKSegmenter;
import com.metis.document.parse.dialog.utils.ikseg.core.Lexeme;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
@Slf4j
public class IkUtils {

    public static List<Lexeme> ikParse(@NotNull String text, @NotNull Properties prop) throws IOException {
        if (StringUtils.isBlank(text)) {
            return Collections.emptyList();
        }
        List<Lexeme> result = new ArrayList<>();
        IKSegmenter segmenter = new IKSegmenter(new StringReader(text), new IkConfiguration(prop));
        Lexeme next;
        while ((next = segmenter.next()) != null) {
            result.add(next);
        }
        return result;
    }
}
